package com.yhans.serviceImpl;


import com.alibaba.dubbo.config.annotation.Service;
import com.yhans.dao.OrderSettingDao;
import com.yhans.pojo.OrderSetting;
import com.yhans.service.OrderSettingService;
import com.yhans.utils.DaysOfMonthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    //批量导入预约设置数据
    public void add(List<OrderSetting> data) {

        if (data!=null && data.size()!=0){
            for (OrderSetting orderSetting : data) {
                //判断当前日期是否已经进行了预约设置
                long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(countByOrderDate > 0){
                    //已经存在，执行更新操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else{
                    //不存在，执行添加操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }

    }

    //根据日期查询预约设置数据(获取指定日期所在月份的预约设置数据)
    public List<Map> getOrderSettingByMonth(String date) {    //2023-4
        String dateBegin = date + "-1";//2023-4-1
        String daysOfMonth = "-"+DaysOfMonthUtils.getDaysOfMonth(date);
        String dateEnd = date + daysOfMonth;//2023-4-31 往最大的算

        Map map = new HashMap();
        map.put("beign",dateBegin);
        map.put("end",dateEnd);

        //获取查询的数据
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);


        //封装成要返回的数据
        List<Map> result = new ArrayList<>();
        if (list!=null && list.size()!=0){
            for (OrderSetting orderSetting : list) {
                Map<String,Object> orderSettingMap = new HashMap<>();
                orderSettingMap.put("date",orderSetting.getOrderDate().getDate());//获取日期中的几号
                orderSettingMap.put("number",orderSetting.getNumber());
                orderSettingMap.put("reservations",orderSetting.getReservations());
                result.add(orderSettingMap);
            }
        }
        return result;
    }

    //根据指定日期修改可预约人数
    public void editNumberByDate(OrderSetting orderSetting) {

        //需要判断当前是否进已经进行了预约
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(count > 0){
            //当前日期已经进行了预约设置，需要进行修改操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //当前日期没有进行预约设置，进行添加操作
            orderSettingDao.add(orderSetting);
        }

    }
}
